import 'dart:convert';
import 'dart:io';
import 'dart:math';

import 'package:device_info/device_info.dart';
import 'package:flutter/material.dart';
import 'package:flutter_encryption/flutter_encryption.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {

  String _entryption = "default";
  String _entryptionF = "default";
  Map<String, String> map = new Map();

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: const Text('Plugin example app'),
        ),
        body: Container(
          child: Column(
            children: [
              Center(
                child: Text('center'),
              ),
              TextButton(
                onPressed: () async {
                  /// 随机函数
                  var rd = new Random();

                  /// 获取设备信息
                  DeviceInfoPlugin deviceInfo = new DeviceInfoPlugin();
                  String deviceId = "";
                  if (Platform.isAndroid) {
                    AndroidDeviceInfo androidInfo = await deviceInfo.androidInfo;
                    deviceId = androidInfo.androidId;
                  } else if (Platform.isIOS) {
                    IosDeviceInfo iosDeviceInfo = await deviceInfo.iosInfo;
                    deviceId = iosDeviceInfo.identifierForVendor;
                  }


                  /// 时间戳 * 随机系数
                  double t =
                      new DateTime.now().millisecondsSinceEpoch * rd.nextDouble();

                  map["_rand "] = t.toString();
                  map["_mach"] = deviceId;
                  map["_token"] = "";

                  String res =
                  (await FlutterEncryption.encode(jsonEncode(map)))!;
                  setState(() {
                    _entryption = res;
                  });

                  debugPrint("res: $res");
                },
                child: Text("加密-$_entryption"),
              ),
              TextButton(
                onPressed: () async {
                  String res =
                  (await FlutterEncryption.decode(_entryption))!;
                  setState(() {
                    _entryptionF = res;
                  });
                  print("res: $res");
                },
                child: Text("解密-$_entryptionF"),
              ),
              TextButton(
                onPressed: () async {
                  String res =
                  (await FlutterEncryption.encode2("15189804929"))!;
                  setState(() {
                    _entryptionF = res;
                  });
                  print("res: $res");
                },
                child: Text("jiami-$_entryptionF"),
              ),
              TextButton(
                onPressed: () async {
                  String res =
                  (await FlutterEncryption.decode2(_entryptionF))!;
                  setState(() {
                    _entryptionF = res;
                  });
                  print("res: $res");
                },
                child: Text("jiemi-$_entryptionF"),
              ),
            ],
          ),
        ),
      ),
    );
  }
}
